package com.hjqjl.whdemo.widget.glowworm;


import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.blankj.utilcode.util.ScreenUtils;
import com.hjqjl.whdemo.utils.ImageUtil;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class Glowworm {
    private float friction = (float) 0.95;//摩擦力
    private Paint paint;
    private int alpha = 255;
    private boolean yueJie = true;
    private float x;
    private float y;
    private float dx;
    private float dy;
    private int n;
    private Bitmap bmpGlowwormZoom;
    //每帧的宽高
    private float frameW, frameH;
    //动画播放当前的帧下标
    private int cureentFrameIndex;
    //效果的总帧数
    private int totleFrame = 9;
    private float denominator;//分母。（因为原图片需要按照一定比例缩小）

    int screenW = ScreenUtils.getAppScreenWidth();
    int screenH = ScreenUtils.getAppScreenHeight();

    public Glowworm(Bitmap bmpGlowworm) {
        paint = new Paint();
        x = new Random().nextInt(screenW);
        y = screenH - new Random().nextInt(screenH / 4);//范围在3/4和4/4之间

        denominator = new Random().nextFloat() * 2 + 2;
        float w = bmpGlowworm.getWidth() / denominator;
        float h = bmpGlowworm.getHeight() / denominator;
        frameW = w / totleFrame;
        frameH = h;
        bmpGlowwormZoom = ImageUtil.zoomBitmap(bmpGlowworm, w, h);
        cureentFrameIndex = new Random().nextInt(totleFrame);
    }

    public void drawSelf(Canvas canvas) {
        paint.setAlpha(alpha);
        canvas.save();
        canvas.clipRect(x, y, x + frameW, y + frameH);
        canvas.drawBitmap(bmpGlowwormZoom, x - cureentFrameIndex * frameW, y, paint);
        canvas.restore();
    }

    public void logicBabysbreath() {
        if (cureentFrameIndex < totleFrame) {
            n++;
            if (n % 9 == 0) {
                if (cureentFrameIndex < (totleFrame - 1)) {
                    cureentFrameIndex++;
                } else {
                    cureentFrameIndex = 0;
                }
            }
        }
        move();
    }

    private Timer timer;
    private TimerTask task;

    private void move() {
        //布朗运动（没看懂）
        dx += (Math.random() * 0.5 - 0.25);
        dy += (Math.random() * 0.5 - 0.25);
        x += dx;
        y += dy;
        dx *= friction;
        dy *= friction;
        if (x < 0 || x > screenW || y > screenH || y < (screenH * 3 / 4)) {
            if (yueJie == true) {
                timer = new Timer();
                task = new TimerTask() {

                    @Override
                    public void run() {
                        alpha -= 30;
                        if (alpha <= 50) {
                            alpha = 255;
                            x = new Random().nextInt(screenW);
                            y = screenH - new Random().nextInt(screenH / 4);
                            yueJie = true;
                            timer.cancel();
                        }
                    }
                };
                timer.schedule(task, 0, 1000);
                yueJie = false;
            }
        }
    }
}
